package com.office.controller;




import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.office.bean.Dictionary;
import com.office.service.DictionaryService;
import com.office.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dictionary")
@CrossOrigin
public class DictionaryController {
    //依赖注入
    @Autowired
    DictionaryService dictionaryService;
    @GetMapping("list")
    public Result getList(){
        List<Dictionary> list = dictionaryService.list();
        return Result.success(list);
    }
    @PostMapping
    public Result add(@RequestBody Dictionary dictionary){
        dictionaryService.save(dictionary);
        Dictionary dictionary1 = dictionaryService.getById(dictionary.getId());
        return Result.success("保存成功",dictionary);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id){
        boolean b = dictionaryService.removeById(id);
        if (b){
            return Result.success("删除成功");
        }
        else {
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody Dictionary dictionary){
        boolean b = dictionaryService.saveOrUpdate(dictionary);
        Dictionary dictionary1 = dictionaryService.getById(dictionary.getId());
        if (b){
            return Result.success("保存成功");
        }
        else {
            return Result.fail(201,"修改失败");
        }
    }

}

